package cn.oa.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.oa.bean.Account;
import cn.oa.bean.Announcement;
import cn.oa.service.AnnouncementService;
@Controller
public class AnnouncementController {
	@Resource
	private AnnouncementService annService;
	
	@GetMapping("/findDepartmentAnn")
	@ResponseBody
	public Map<String, Object> findDepartmentMsg(ModelMap map){
		return null;
	}
	@GetMapping("/findAllAnn")
	@ResponseBody
	public Map<String, Object> findAllMsg(ModelMap map){
		List list = annService.findAllAnn();
		map.put("result",list);
		Map<String, Object> hashmap = new HashMap<>();
		hashmap.put("DepartmentMsg",list);
		return hashmap;
	}
	@GetMapping("/msg")
	public String allAnn(ModelMap map,HttpServletRequest req){
		HttpSession session = req.getSession();
		Object account = session.getAttribute("Account");
		System.out.println(account);
		List list = annService.findDepartmentAnn((Account)account);
		map.addAttribute("DepartmentMsg",list);
		map.addAttribute("result",annService.findAllAnn());
		return "/msgindex";
	}
	@PostMapping("/addAnn")
	public String addAnn(ModelMap map,HttpServletRequest req,Announcement ann){
		HttpSession session = req.getSession();
		Account account = (Account) session.getAttribute("Account");
		if(account.getId()==annService.query(account)){
			ann.setUserId(account.getId());
			Date data = new Date();
			ann.setCreateTime(data);
			annService.addAnn(ann);
			map.addAttribute("errorMessage", "发布成功");
			return "redirect:/msg";
		}else{
			map.addAttribute("errorMessage", "您不是部门主管或公司高层，不能发布公告");
			return "redirect:/msg";
		}
		
	}
}
